unit QBProc;

{$I QBuilderVerCtrl.inc}

interface

uses
  {$IFDEF VCL16}
    Winapi.Windows,
    Vcl.Controls,
    Vcl.Graphics,
    Data.Db,
  {$ELSE}
    Windows,
    Controls,
    Graphics,
    Db,
  {$ENDIF}
  QBFloatPanel,
  QBFloatTable,
  QBStringTypes;
type
  TQBStoredProc = class(TQBTable)
  private
    FParams: TParams;
  protected
    function GetClientColor: TColor; override;
  public
    constructor Create(AParent: TQBWorkArea; ADataSet: TDataSet; AName: aqbString;
      AParams: TParams);
    destructor Destroy; override;
    //    function GetParameters: aqbString;
  end;

implementation

uses
  {$IFDEF VCL16}
    System.SysUtils,
    System.Variants,
  {$ELSE}
    SysUtils,
    {$IFDEF VCL6}
      Variants,
    {$ENDIF}
  {$ENDIF}
  QBWindow,
  QBParser;

constructor TQBStoredProc.Create(AParent: TQBWorkArea; ADataSet: TDataSet;
  AName: aqbString; AParams: TParams);
var
  Item: TQBListItem;
  i, C: Integer;
begin
  inherited Create(AParent, ADataSet, AName);
  FParams := TParams.Create;
  if Assigned(AParams) then
  begin
    FParams.Assign(AParams);
    for i := 0 to AParams.Count - 1 do
      FParams[i].Value := AParams[i].Value;
  end;

  //adding params
  C := 0;
  for i := 0 to FParams.Count - 1 do
  begin
    if not (FParams[I].ParamType in QBParamTypes) then
      Continue;

    Inc(C);
    Item := TQBListItem.Create(Self, FParams[I].Name, FParams[I].Name,
      FParams[I].DataType, ptInput);
    FItems.Add(Item);
    if not VarIsEmpty(FParams[I].Value) then
    begin
      Item.Locked := True;
      try
        Item.Checked := True;
      finally
        Item.Locked := False;
      end;
      Item.IsNull := FParams[I].IsNull;
      Item.VarValue := FParams[I].Value;
    end;
  end;
  if FCountVisibleItem < 7 then
    if C + FCountVisibleItem > 6 then
      FCountVisibleItem := 6
    else
      FCountVisibleItem := C + FCountVisibleItem;
  FHeight := FCountVisibleItem * (FItemHeight + 1) + FCaptionHeight +
    FBorderWidth + 2 * FBevelWidth;
end;

destructor TQBStoredProc.Destroy;
begin
  if Assigned(FParams) then
  begin
    FParams.Clear;
    FParams.Free;
  end;
  inherited;
end;

function TQBStoredProc.GetClientColor: TColor;
begin
  Result := TCustomQueryBuilder(Parent.Parent).Palette.ProcClient;
end;

{function TQBStoredProc.GetParameters: aqbString;
var
  i: Integer;
  Item: TQBListItem;

  function ChangeSeparator(Str: aqbString): aqbString;
  var
    i: Integer;
  begin
    Result := Str;
    for i := 1 to Length(Result) do
      if Result[i] = DecimalSeparator then Result[i] := '.';
  end;

  function GetParamName(AName: aqbString): aqbString;
  var
    j: Integer;
    s: aqbString;
  begin
    Result := '';
    s := AnsiUpperCase(AName);
    for j := 1 to Length(s) do
    begin
      if s[j] in ['$', '0'..'9', 'A'..'Z', '_'] then
        Result := Result + s[j]
      else if s[j] = ' ' then
        Result := Result + '_';
    end;
    if Result = '' then Result := 'Par' + IntToStr(i);
  end;
begin
  Result := '';
  for i := 0 to FItems.Count - 1 do
  begin
    Item := FItems.Items[i];
    if Item.ScopeType <> ptInput then Continue;
    if not Item.Checked then
      Result := Result + ':' + GetParamName(Item.Caption)
    else if Item.IsNull then
      Result := Result + 'NULL'
    else
      case VarType(Item.VarValue) of
        varEmpty: Result := Result + ':' + GetParamName(Item.Caption);
        varNull: Result := Result + 'NULL';
        varSmallint,
          varInteger: Result := Result + IntToStr(Item.VarValue);
        varSingle,
          varDouble,
          varCurrency: Result := Result +
            ChangeSeparator(FloatToStr(Item.VarValue));
        varDate:
          if Item.DataType = Db.ftDate then
            Result := Result + '''' + DateToStr(Item.VarValue) + ''''
          else if Item.DataType = Db.ftTime then
            Result := Result + '''' + TimeToStr(Item.VarValue) + ''''
          else
            Result := Result + '''' + DateTimeToStr(Item.VarValue) + '''';
        varOleStr,
          varStrArg,
          varString: Result := Result + '''' + Item.VarValue + '''';
        varBoolean:
          if Item.VarValue then
            Result := Result + 'T'
          else
            Result := Result + 'F'
        else
          Continue;
      end; (**)
    Result := Result + ', ';
  end;
  i := Length(Result);
  if i > 0 then Delete(Result, i - 1, 2);
end;}

end.
